package com.shujia.controller;

import com.shujia.entity.Emp;
import com.shujia.entity.User;
import com.shujia.service.impl.EmpServiceImpl;
import com.shujia.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private EmpServiceImpl empService;

    //登录页面，是访问项目的主页面
    @GetMapping("/index.html")
    public String userLogin() {
        return "login";
    }


    @RequestMapping("/login")
    public String login(User user, Model model) {
        //取查询数据库
        User user1 = userService.findByNameAndPassword(user.getUsername(), user.getPassword());
        if (user1 != null) {
            //如果查询的用户存在不是空，说明用户登录成功，跳转到首页index.html
            //如果登录成功，查询emp表，将emp表的结果显示在页面上
            List<Emp> emps = empService.findAll();
            model.addAttribute("emps", emps);
            return "index";
        } else {
            System.out.println("登录失败！");
            return "login";
        }

    }

}
